package com.demo;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Title: HikariDataSourceTest
 * @Description: [hakari连接池使用]
 * @Author lei.cheng
 * @Date 2024/12/4
 */
public class HikariDataSourceTest {
    public static void main(String[] args) throws SQLException {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        HikariDataSource hikariDataSource = new HikariDataSource(config);
        Connection connection = hikariDataSource.getConnection();
        Statement statement = connection.createStatement();

        String sql = "SELECT * FROM sys_config";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.print("variable:" + resultSet.getString("variable"));
            System.out.print(", value:" + resultSet.getString("value"));
            System.out.print(", set_time:" + resultSet.getString("set_time"));
            System.out.print(", set_by:" + resultSet.getString("set_by"));
            System.out.println();
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
